package parser

import (
	"qweewq.tk/example/crawier/engine"
	"qweewq.tk/example/crawier/model"
	"regexp"
)

const zhenaiUserItemGroupReg = `<div class="list-item">(.*?)<div class="item-btn">打招呼</div></div>`
const zhenaiUserItemReg = `<div class="photo"><a href="(.*?)" target="_blank"><img src="(.*?)" alt="(.*?)"></a></div>.*?<div class="content">(.*?)</div>`

const zhenaiUserContentTable = `<table><tbody>(.*?)</tbody></table>`
const zhenaiUserContentGenderReg = `<tr><td width=".*?"><span class="grayL">性别：</span>(.*?)</td>`
const zhenaiUserContentAddressReg = `<td><span class="grayL">居住地：</span>(.*?)</td>`
const zhenaiUserContentOldReg = `<td width=".*?"><span class="grayL">年龄：</span>(.*?)</td>.`
const zhenaiUserContentEduReg = `<td><span class="grayL">学.*?历：</span>(.*?)</td>`
const zhenaiUserContentCivilStatus = `<td width=".*?"><span class="grayL">婚况：</span>(.*?)</td>`
const zhenaiUserContentStature = `<td width=".*?"><span class="grayL">身.*?高：</span>(.*?)</td>`
const zhenaiUserSalary = `<td><span class="grayL">月.*?薪：</span>(.*?)</td>`
const zhenaiUserRemark = `<div class="introduce">(.*?)<br/>`

func ParserUserContent(content []byte) *engine.ParserResult {
	//找出每一个用户的table
	userTableReg := regexp.MustCompile(zhenaiUserContentTable)
	matches := userTableReg.FindAllSubmatch(content, -1)

	userGenderReg := regexp.MustCompile(zhenaiUserContentGenderReg)
	userAddressReg := regexp.MustCompile(zhenaiUserContentAddressReg)
	userOldReg := regexp.MustCompile(zhenaiUserContentOldReg)
	userEduReg := regexp.MustCompile(zhenaiUserContentEduReg)
	userCivilStatusReg := regexp.MustCompile(zhenaiUserContentCivilStatus)
	userStatureReg := regexp.MustCompile(zhenaiUserContentStature)
	userSalaryReg := regexp.MustCompile(zhenaiUserSalary)
	userRemarkReg := regexp.MustCompile(zhenaiUserRemark)

	var requests []engine.Request
	var userItems []engine.Item
	for _, elem := range matches {
		//匹配每一个用户的信息项
		var item = new(model.UserItem)

		genderMatches := userGenderReg.FindAllSubmatch(elem[1], -1)
		if genderMatches != nil {
			item.Gender = string(genderMatches[0][1])
		}
		addressMatches := userAddressReg.FindAllSubmatch(elem[1], -1)
		if addressMatches != nil {
			item.Address = string(addressMatches[0][1])
		}

		oldMatches := userOldReg.FindAllSubmatch(elem[1], -1)
		if oldMatches != nil {
			item.Old = string(oldMatches[0][1])
		}

		eduMatches := userEduReg.FindAllSubmatch(elem[1], -1)
		if eduMatches != nil {
			item.Education = string(eduMatches[0][1])
		}

		civilStatusMatches := userCivilStatusReg.FindAllSubmatch(elem[1], -1)
		if civilStatusMatches != nil {
			item.CivilStatus = string(civilStatusMatches[0][1])
		}

		statureMatches := userStatureReg.FindAllSubmatch(elem[1], -1)
		if statureMatches != nil {
			item.Stature = string(statureMatches[0][1])
		}

		salaryMatches := userSalaryReg.FindAllSubmatch(elem[1], -1)
		if salaryMatches != nil {
			item.Salary = string(salaryMatches[0][1])
		}

		remarkMatches := userRemarkReg.FindAllSubmatch(elem[1], -1)
		if remarkMatches != nil {
			item.Remark = string(remarkMatches[0][1])
		}

		userItems = append(userItems, item)
	}
	return &engine.ParserResult{
		Requests: requests,
		Items:    userItems,
	}
}

func ParserUserItem(content []byte) *engine.ParserResult {
	reg := regexp.MustCompile(zhenaiUserItemReg)
	matches := reg.FindAllSubmatch(content, -1)

	var requests []engine.Request
	var items []engine.Item

	for _, elem := range matches {
		//组装数据到items中
		userContent := ParserUserContent(elem[4])
		for i := 0; i < len(userContent.Items); i++ {
			switch item := userContent.Items[i].(type) {
			case *model.UserItem:
				item.HomeUrl = string(elem[1])
				item.PhotoUrl = string(elem[2])
				item.Name = string(elem[3])
			}
		}
		items = append(items, userContent.Items...)
	}
	return &engine.ParserResult{
		Requests: requests,
		Items:    items,
	}
}

//ParserUserItemGroup 解析city页面中用户组,得到每一个用户项
func ParserUserItemGroup(content []byte) *engine.ParserResult {
	reg := regexp.MustCompile(zhenaiUserItemGroupReg)
	matches := reg.FindAllSubmatch(content, -1)

	var requests []engine.Request
	var items []engine.Item

	for _, elem := range matches {
		userItem := ParserUserItem(elem[1])
		items = append(items, userItem.Items...)
	}
	return &engine.ParserResult{
		Requests: requests,
		Items:    items,
	}
}
